<template>
  <div class="login-container">
    <div class="bg-circle">
      <img
        class="lt"
        src="https://img.axureshop.com/7a/4a/a8/7a4aa826e79b40d086fba67fb100e0f9/images/0登陆页/u3.png"
        alt=""
      >
      <img
        class="lb"
        src="https://img.axureshop.com/7a/4a/a8/7a4aa826e79b40d086fba67fb100e0f9/images/0登陆页/u5.png"
        alt=""
      >
      <img
        class="rt"
        src="https://img.axureshop.com/7a/4a/a8/7a4aa826e79b40d086fba67fb100e0f9/images/0登陆页/u4.png"
        alt=""
      >
    </div>
    <div class="main">
      <img
        src="https://img.axureshop.com/7a/4a/a8/7a4aa826e79b40d086fba67fb100e0f9/images/0登陆页/u25.png"
        alt=""
      >
      <el-card shadow="never">
        <div class="title">
          <img
            src="https://img.axureshop.com/7a/4a/a8/7a4aa826e79b40d086fba67fb100e0f9/images/0登陆页/u24.png"
            alt=""
          >
          <h2>电商后台管理</h2>
        </div>
        <!--登录表单-->
        <el-form ref="loginForm" :model="loginForm" :rules="formRules">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="账户"
              prefix-icon="el-icon-user"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="密码"
              show-password
              prefix-icon="el-icon-lock"
            />
          </el-form-item>
          <el-form-item prop="captcha">
            <el-input v-model="loginForm.captcha" placeholder="验证码" prefix-icon="el-icon-help">
              <template slot="append"><span @click="captchaAdd">{{ cap }}</span></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">用户平台协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              @click="loginAddFn"
            >登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        isAgree: false,
        captcha: ''
      },
      cap: '',
      formRules: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度为6-16位之间', trigger: 'blur' }
        ],
        isAgree: [
          { validator: (rule, value, callback) => {
            value ? callback() : callback(new Error('请勾选用户协议'))
          }
          }
        ],
        captcha: [
          { validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请输入验证码'))
            } else if (value !== this.cap) {
              callback(new Error('验证码错误'))
              console.log(value, this.cap, rule)
            }
          }
          }
        ]
      }
    }
  },
  created() {
    return this.captchaAdd()
  },
  methods: {
    loginAddFn() {
      this.$refs.loginForm.validator((isOk) => {
        if (isOk) {
          this.$store.dispatch('user/loginAction', this.loginForm)
        }
      })
    },

    // loginClick() {
    //   this.$refs.loginForm.validate((isOK) => {
    //     if (isOK) {
    //       this.$store.dispatch('user/loginAction', this.loginForm)
    //     }
    //   })
    // }
    captchaAdd() {
      this.cap = ''
      for (let i = 0; i < 4; i++) {
        const a = Math.floor(Math.random() * 10)
        this.cap += a
      }
    }
  }
}
</script>
<style lang="scss">
.login-container {
  position: relative;
  height: 100%;
  background-image: linear-gradient(#3fccfd, #996cfb);
  overflow: hidden;
  // 三个圆形图⽚的位置
  .bg-circle {
    .lt {
      position: absolute;
      right: 500px;
      top: 5px;
      opacity: 0.3;
    }
    .lb {
      position: absolute;
      right: 20px;
      bottom: -250px;
    }
    .rt {
      position: absolute;
      left: -100px;
      top: -200px;
    }
  }
  // 主体内容
  .main {
    // ⽔平和垂直居中
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    // 登录卡⽚
    .el-card {
      margin-left: 60px;
      // 卡⽚标题
      .title {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 23px;
        font-weight: 700;
        font-style: normal;
        color: rgb(67, 141, 238);
        img {
          width: 50px;
          height: 42px;
        }
      }
      // 登录表单⽂本框
      .el-input {
        width: 350px;
        height: 44px;
        // ⽂本框内容
        .el-input__inner {
          padding-left: 40px;
          font-size: 14px;
          color: #000;
        }
        // ⽂本框头部图标样式
        .el-input__icon {
          font-size: 22px;
          color: #666;
          font-weight: 700;
        }
        // ⽂本框尾部验证码样式
        .el-input-group__append {
          font-family: "Arial Negreta", "Arial Normal", "Arial";
          font-weight: 700;
          font-size: 16px;
          color: #ff7733;
          // height: 44px;
        }
      }
      // 复选款样式
      .el-checkbox {
        color: #606266;
        margin-bottom: 10px;
      }
      // 登录按钮样式
      .login-btn {
        width: 350px;
        border-radius: 10px;
      }
    }
  }
}
</style>
